<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .box1 {
        /* https://www.cdkgg.com/css3/2D/2D-displacement.html#%E4%B9%A6%E5%86%99%E8%AF%AD%E6%B3%95 */
        position: relative;
        top: 100px;
        left: 50%;
        transform: translate(-50%);
        width: 100px;
        height: 100px;
        background-color: red;
      }
      .box2 {
        /* https://www.cdkgg.com/cssbase/Position/Positioning-use.html#%E5%B1%85%E4%B8%AD */
        position: relative;
        top: 200px;
        left: 50%;
        /* padding: 0 20px; */
        margin-left: -50px;
        width: 100px;
        height: 100px;
        background-color: skyblue;
      }
      .box3 {
        /* https://www.cdkgg.com/cssbase/boxexpand/center.html */
        border: 5px solid yellowgreen;
        width: 200px;
        height: 100px;
        position: relative;
        margin: 300px auto;
      }
      .box3 p {
        background-color: pink;
        width: 100px;
        height: 100px;
        left: 50px;
        float: left;
        /* left: 50%; */
        /* margin-left: -50px; */
        position: relative;
      }
    </style>
  </head>
  <body>
    <div class="box1">Box1居中</div>
    <div class="box2">Box2居中</div>
    <div class="box3">
      <p>Box3居中</p>
    </div>
  </body>
</html>
